import Vue from 'vue'
import Vuex, { Commit, Dispatch } from 'vuex'
import user from './user.module'
import order from './order.modules'
import product from './product.modules'
import db from '@/components/db'
import AutoSaverConfig from '@/models/AutoSaverConfig';
import FtpConfig from '@/models/FtpConfig';

Vue.use(Vuex)

export default new Vuex.Store({
    modules: {
        user,
        order,
        product
    },
    state: {
        dbVersion: '',
        autoSaveConfig: AutoSaverConfig,
        ftpConfig: FtpConfig,
        isAdmin: false
    },
    getters: {
        dbVersion(state: any) {
            return state.dbVersion;
        },
        autoSaveConfig(state: any) {
            state.autoSaveConfig = db.getAutoSaverConfig();
            return state.autoSaveConfig;
        },
        isAdmin(state: any) {
            return state.isAdmin;
        },
        ftpConfig(state: any) {
            state.ftpConfig = db.getFtpConfig();
            return state.ftpConfig;
        }
    },
    actions: {
        clearData() {
            return db.reset();
        },
        checkAdminPassword(context: any, p: string) {
            return new Promise((resolve, reject) => {
                db.get('adminPassword').then((result) => {
                    console.log(result);
                    if (p === result) {
                        resolve()
                    } else {
                        reject(new Error("密码有误"));
                    }
                })
            })
        },
        changeAdminPassword(context: any, p: string) {
            return new Promise((resolve, reject) => {
                if (p === '') {
                    reject(new Error("密码不能为空"));
                } else {
                    db.set('adminPassword', p).then(() => {
                        resolve()
                    }).catch(reason => {
                        reject(reason);
                    })
                }
            })
        },
        selectAutoSavePath(context: any) {
            return new Promise((resolve, reject) => {
                db.selectAutoSavePath().then(path => {
                    resolve(path);
                }).catch(reason => {
                    reject(reason);
                })
            })
        },
        saveOnExit() {
            return new Promise((resolve, reject) => {
                resolve(db.saveOnExit())
            })
        },
        importImageToLocal() {
            return new Promise((resolve) => {
                resolve(db.importImageToLocal());
            })
        },
        deleteImage(context: any, image: string) {
            return new Promise((resolve, reject) => {
                resolve(db.deleteFile(image));
            })
        }
    },
    mutations: {
        setDbVersion(state: any, version: string) {
            state.dbVersion = version;
        },
        setAutoSaverConfig(state: any, c: any) {
            const config = new AutoSaverConfig(c);
            state.autoSaveConfig = config;
            db.setAutoSaverConfig(config);
            db.restartAutoSaver();
        },
        setFtpConfig(state: any, c: any) {
            const config = new FtpConfig(c);
            state.ftpConfig = config;
            db.setFtpConfig(config);
        },
        setAdmin(state: any, value: boolean) {
            state.isAdmin = value;
        }
    }
})